package com.study.entity;

import com.alibaba.fastjson.JSON;
import com.study.constant.GoodsStatus;
import com.study.goods.GoodsInfo;
import com.study.goods.SimpleGoodsInfo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.Date;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(name = "t_ecommerce_goods")
@SuppressWarnings("all")
public class EcommerceGoods {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    private Long id;

    //商品类别
    @Column(name = "good_category", nullable = false)
    private Integer goodsCategory;

    //品牌分类
    @Column(name = "brand_category", nullable = false)
    private Integer brandCategory;

    //商品名称
    @Column(name = "goods_name", nullable = false)
    private String goodsName;

    @Column(name = "goods_pic", nullable = false)
    private String goodsPic;

    @Column(name = "goods_description", nullable = false)
    private String goodsDescription;

    @Column(name = "goods_status", nullable = false)
    private Integer goodsStatus;

    //商品价格 单位元
    @Column(name = "price", nullable = false)
    private BigDecimal price;

    //总供应量
    @Column(name = "supply", nullable = false)
    private Integer supply;

    //库存
    @Column(name = "inventory", nullable = false)
    private Integer inventory;

    //商品属性 json字符串存储
    @Column(name = "goods_property", nullable = false)
    private String goodsProperty;

    @CreatedDate
    @Column(name = "create_time", nullable = false)
    private Date createTime;

    @LastModifiedDate
    @Column(name = "update_time", nullable = false)
    private Date updateTime;

    public static EcommerceGoods to(GoodsInfo goodsInfo) {

        EcommerceGoods ecommerceGoods = new EcommerceGoods();
        ecommerceGoods.setGoodsCategory(goodsInfo.getGoodsCategory());
        ecommerceGoods.setBrandCategory(goodsInfo.getBrandCategory());
        ecommerceGoods.setGoodsName(goodsInfo.getGoodsName());
        ecommerceGoods.setGoodsPic(goodsInfo.getGoodsPic());
        ecommerceGoods.setGoodsDescription(goodsInfo.getGoodsDescription());
        ecommerceGoods.setGoodsStatus(GoodsStatus.ONLINE.getStatus());
        ecommerceGoods.setPrice(goodsInfo.getPrice());
        ecommerceGoods.setSupply(goodsInfo.getSupply());
        ecommerceGoods.setInventory(goodsInfo.getSupply());
        ecommerceGoods.setGoodsProperty(
                JSON.toJSONString(goodsInfo.getGoodsProperty())
        );
        return ecommerceGoods;
    }

    public GoodsInfo toGoodsInfo() {
        GoodsInfo goodsInfo = new GoodsInfo();
        goodsInfo.setId(this.id);
        goodsInfo.setGoodsCategory(this.goodsCategory);
        goodsInfo.setBrandCategory(this.brandCategory);
        goodsInfo.setGoodsName(this.getGoodsName());
        goodsInfo.setGoodsPic(this.goodsPic);
        goodsInfo.setGoodsDescription(this.goodsDescription);
        goodsInfo.setGoodsStatus(this.goodsStatus);
        goodsInfo.setPrice(this.price);
        //json反序列化
        goodsInfo.setGoodsProperty(
                JSON.parseObject(this.goodsProperty, GoodsInfo.GoodsProperty.class)
        );
        goodsInfo.setSupply(this.supply);
        goodsInfo.setInventory(this.inventory);
        goodsInfo.setCreateTime(this.createTime);
        goodsInfo.setUpdateTime(this.updateTime);

        return goodsInfo;
    }

    public SimpleGoodsInfo toSimple() {

        SimpleGoodsInfo simpleGoodsInfo = new SimpleGoodsInfo();
        simpleGoodsInfo.setId(this.id);
        simpleGoodsInfo.setGoodsName(this.goodsName);
        simpleGoodsInfo.setGoodsPic(this.goodsPic);
        simpleGoodsInfo.setPrice(this.price);

        return simpleGoodsInfo;

    }


}
